/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import ClasesBase.TextFilter;
import Extras.GestorEstilos;
import Extras.Mensajes;
import Gestores.GestorCliente;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;

/**
 *
 * @author Francisco
 */
public class UnirseAPartidaD extends javax.swing.JDialog {

    private GestorCliente gestorCliente;
    private PrincipalF principalF;

    /**
     * Creates new form UnirseAPartidaD
     */
    public UnirseAPartidaD(PrincipalF parent, boolean modal, GestorCliente gestorCliente) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        this.principalF = parent;
        this.gestorCliente = gestorCliente;
        this.gestorCliente.setUnirseAPartidaD(this);
        GestorEstilos.pintar(this);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTF_nickname = new javax.swing.JTextField();
        jB_cancelar = new javax.swing.JButton();
        jB_aceptar = new javax.swing.JButton();
        jLe_nickname = new javax.swing.JLabel();
        jTF_ipServidor = new javax.swing.JTextField();
        jLe_ipServidor = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Unirse a Partida");
        setMaximumSize(new java.awt.Dimension(300, 120));
        setMinimumSize(new java.awt.Dimension(300, 120));
        setPreferredSize(new java.awt.Dimension(315, 150));
        setResizable(false);
        getContentPane().setLayout(null);

        jTF_nickname.setDocument(new TextFilter(TextFilter.LETTERS));
        jTF_nickname.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jTF_nickname.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTF_nicknameKeyPressed(evt);
            }
        });
        getContentPane().add(jTF_nickname);
        jTF_nickname.setBounds(96, 11, 195, 22);

        jB_cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Botones/CANCELAR ON.png"))); // NOI18N
        jB_cancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jB_cancelarMouseReleased(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jB_cancelarMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jB_cancelarMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jB_cancelarMousePressed(evt);
            }
        });
        jB_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_cancelarActionPerformed(evt);
            }
        });
        jB_cancelar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jB_cancelarKeyPressed(evt);
            }
        });
        getContentPane().add(jB_cancelar);
        jB_cancelar.setBounds(180, 90, 80, 22);

        jB_aceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Botones/ACEPTAR ON.png"))); // NOI18N
        jB_aceptar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jB_aceptarMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jB_aceptarMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jB_aceptarMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jB_aceptarMouseReleased(evt);
            }
        });
        jB_aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_aceptarActionPerformed(evt);
            }
        });
        jB_aceptar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jB_aceptarKeyPressed(evt);
            }
        });
        getContentPane().add(jB_aceptar);
        jB_aceptar.setBounds(30, 90, 80, 22);

        jLe_nickname.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLe_nickname.setText("Nickname:");
        getContentPane().add(jLe_nickname);
        jLe_nickname.setBounds(10, 14, 71, 15);

        jTF_ipServidor.setDocument(new TextFilter(TextFilter.IP_NUMBER));
        jTF_ipServidor.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jTF_ipServidor.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTF_ipServidorKeyPressed(evt);
            }
        });
        getContentPane().add(jTF_ipServidor);
        jTF_ipServidor.setBounds(96, 54, 195, 22);

        jLe_ipServidor.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLe_ipServidor.setText("IP Servidor:");
        getContentPane().add(jLe_ipServidor);
        jLe_ipServidor.setBounds(10, 57, 76, 15);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jB_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_cancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_jB_cancelarActionPerformed

    private void jB_aceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_aceptarActionPerformed
        String nickname = this.jTF_nickname.getText();
        String ipServidor = this.jTF_ipServidor.getText();
        if (nickname.isEmpty() || ipServidor.isEmpty()) {
            Mensajes.mostrarInformacion("Debe completar ambos campos.");
            return;
        }
        if (nickname.length() >= 12) {
            Mensajes.mostrarInformacion("El maximo de tamaño permitido para el nickname es 12 caracteres. Por favor intente nuevamente.");
            return;
        }
        if(nickname.trim().compareToIgnoreCase("Banco") == 0){
            Mensajes.mostrarInformacion("El nickname \"Banco\" se encuentra reservado. Por favor elija otro.");
            return;
        }
        gestorCliente.setIpServidor(ipServidor);
        gestorCliente.setNickname(nickname.trim());
        gestorCliente.iniciarConexion();
        gestorCliente.crearEnEspera();
    }//GEN-LAST:event_jB_aceptarActionPerformed

    private void jB_aceptarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jB_aceptarKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            this.jB_aceptarActionPerformed(null);
        }
    }//GEN-LAST:event_jB_aceptarKeyPressed

    private void jB_cancelarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jB_cancelarKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            this.dispose();
        }
    }//GEN-LAST:event_jB_cancelarKeyPressed

    private void jTF_nicknameKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTF_nicknameKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            this.jB_aceptarActionPerformed(null);
        }
    }//GEN-LAST:event_jTF_nicknameKeyPressed

    private void jTF_ipServidorKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTF_ipServidorKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            this.jB_aceptarActionPerformed(null);
        }
    }//GEN-LAST:event_jTF_ipServidorKeyPressed

    private void jB_aceptarMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jB_aceptarMouseEntered
        jB_aceptar.setIcon(new ImageIcon(getClass().getResource("/Imagenes/Botones/ACEPTAR SEL.png")));
    }//GEN-LAST:event_jB_aceptarMouseEntered

    private void jB_aceptarMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jB_aceptarMouseExited
       jB_aceptar.setIcon(new ImageIcon(getClass().getResource("/Imagenes/Botones/ACEPTAR ON.png")));
    }//GEN-LAST:event_jB_aceptarMouseExited

    private void jB_aceptarMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jB_aceptarMousePressed
        jB_aceptar.setIcon(new ImageIcon(getClass().getResource("/Imagenes/Botones/ACEPTAR OFF.png")));
    }//GEN-LAST:event_jB_aceptarMousePressed

    private void jB_aceptarMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jB_aceptarMouseReleased
       jB_aceptar.setIcon(new ImageIcon(getClass().getResource("/Imagenes/Botones/ACEPTAR ON.png")));
    }//GEN-LAST:event_jB_aceptarMouseReleased

    private void jB_cancelarMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jB_cancelarMouseEntered
        jB_cancelar.setIcon(new ImageIcon(getClass().getResource("/Imagenes/Botones/CANCELAR SEL.png")));
    }//GEN-LAST:event_jB_cancelarMouseEntered

    private void jB_cancelarMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jB_cancelarMouseExited
        jB_cancelar.setIcon(new ImageIcon(getClass().getResource("/Imagenes/Botones/CANCELAR ON.png")));
    }//GEN-LAST:event_jB_cancelarMouseExited

    private void jB_cancelarMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jB_cancelarMousePressed
        jB_cancelar.setIcon(new ImageIcon(getClass().getResource("/Imagenes/Botones/CANCELAR OFF.png")));
    }//GEN-LAST:event_jB_cancelarMousePressed

    private void jB_cancelarMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jB_cancelarMouseReleased
        jB_cancelar.setIcon(new ImageIcon(getClass().getResource("/Imagenes/Botones/CANCELAR ON.png")));
    }//GEN-LAST:event_jB_cancelarMouseReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jB_aceptar;
    private javax.swing.JButton jB_cancelar;
    private javax.swing.JLabel jLe_ipServidor;
    private javax.swing.JLabel jLe_nickname;
    private javax.swing.JTextField jTF_ipServidor;
    private javax.swing.JTextField jTF_nickname;
    // End of variables declaration//GEN-END:variables

   
    public Image getIconImage() {
        Image retValue = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("Imagenes/icono.png"));
        return retValue;
    }
}
